/*
 * TelaVeiculo.java
 *
 * Created on __DATE__, __TIME__
 */

package telas.aluguel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.text.MaskFormatter;
import business.aluguel.Veiculo;
import business.sistema.ExceptionSistema;
import business.sistema.IObjetoSistema;
import telas.comum.TelaMDI;

/**
 *
 * @author  __USER__
 */
public class TelaVeiculo extends TelaMDI
{
	private static final long serialVersionUID = -3193578449038460039L;

	/** Creates new form TelaVeiculo */
	public TelaVeiculo() throws Exception
	{
		initComponents();

		eventoBuscar();
		eventoNovo();
		eventoEditar();
		eventoExcluir();
	}

	private void eventoExcluir()
	{
		barraFerramenta1.getBtExcluir().addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					exluirVeiculo();
				} catch (Exception ex)
				{
					new ExceptionSistema(ex);
				}
			}
		});
	}

	private void exluirVeiculo() throws Exception
	{
		Veiculo veiculo = (Veiculo) tblVeiculos.getObjetoCorrente();

		veiculo.excluir();

		preencheTabelaVeiculos();
	}

	private void eventoEditar()
	{
		barraFerramenta1.getBtEditar().addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					abrirTela(false);
				} catch (Exception ex)
				{
					new ExceptionSistema(ex);
				}
			}
		});
	}

	private void eventoNovo()
	{
		barraFerramenta1.getBtNovo().addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					abrirTela(true);
				} catch (Exception ex)
				{
					new ExceptionSistema(ex);
				}
			}
		});
	}

	private void abrirTela(boolean novo) throws Exception
	{
		Veiculo veiculo;

		if (novo)
		{
			veiculo = new Veiculo();
		} else
		{
			veiculo = (Veiculo) tblVeiculos.getObjetoCorrente();
		}

		new TelaVeiculoCadastro(veiculo, novo);

		preencheTabelaVeiculos();
	}

	private void eventoBuscar()
	{
		barraFerramenta1.getBtBusca().addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					preencheTabelaVeiculos();
				} catch (Exception ex)
				{
					new ExceptionSistema(ex);
				}
			}
		});
	}

	private void preencheTabelaVeiculos() throws Exception
	{
		List<IObjetoSistema> acessorios = new Veiculo()
				.obterTodosPorFiltro(filtro());

		tblVeiculos.setDados(acessorios);
		tblVeiculos.setColunas(colunasTabelaVeiculos());
		tblVeiculos.bind();
	}

	private String filtro() throws Exception
	{
		StringBuffer criterio = new StringBuffer("1 = 1");

		String placa = ctPlaca.getText();
		if (!placa.equals("") && !placa.equals("   -    "))
		{
			criterio.append(" AND placa LIKE '%" + ctPlaca.getText() + "%'");
		}

		return criterio.toString();
	}

	private ArrayList<String[]> colunasTabelaVeiculos()
	{
		ArrayList<String[]> dscColunas = new ArrayList<String[]>();
		dscColunas.add(new String[]
		{ "Placa", "getPlaca", "30" });
		dscColunas.add(new String[]
		{ "Veículo", "getNome" });

		return dscColunas;
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() throws Exception
	{

		pnlPrincipal = new telas.componentes.Painel();
		barraFerramenta1 = new telas.componentes.BarraFerramenta();
		pnlCadastro = new telas.componentes.Painel();
		pnlFiltro = new telas.componentes.Painel();
		rotulo1 = new telas.componentes.Rotulo();
		ctPlaca = new telas.componentes.CampoTexto(
				new MaskFormatter("###-####"));
		jspTabela = new javax.swing.JScrollPane();
		tblVeiculos = new telas.componentes.Tabela();

		setTitle("Ve\u00edculos");

		pnlPrincipal.setLayout(new java.awt.BorderLayout());

		barraFerramenta1.setRollover(true);
		pnlPrincipal.add(barraFerramenta1, java.awt.BorderLayout.PAGE_START);

		pnlCadastro.setLayout(new java.awt.BorderLayout());

		pnlFiltro.setBorder(javax.swing.BorderFactory
				.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

		rotulo1.setText("Placa:");

		javax.swing.GroupLayout pnlFiltroLayout = new javax.swing.GroupLayout(
				pnlFiltro);
		pnlFiltro.setLayout(pnlFiltroLayout);
		pnlFiltroLayout.setHorizontalGroup(pnlFiltroLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				pnlFiltroLayout.createSequentialGroup().addContainerGap()
						.addComponent(rotulo1,
								javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE).addGap(
								17, 17, 17).addComponent(ctPlaca,
								javax.swing.GroupLayout.PREFERRED_SIZE, 99,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(510, Short.MAX_VALUE)));
		pnlFiltroLayout
				.setVerticalGroup(pnlFiltroLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								pnlFiltroLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												pnlFiltroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																rotulo1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																ctPlaca,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		pnlCadastro.add(pnlFiltro, java.awt.BorderLayout.PAGE_START);

		jspTabela.setBorder(javax.swing.BorderFactory
				.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

		tblVeiculos.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][]
				{

				}, new String[]
				{

				}));
		jspTabela.setViewportView(tblVeiculos);

		pnlCadastro.add(jspTabela, java.awt.BorderLayout.CENTER);

		pnlPrincipal.add(pnlCadastro, java.awt.BorderLayout.CENTER);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				pnlPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 672,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				pnlPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 378,
				Short.MAX_VALUE));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private telas.componentes.BarraFerramenta barraFerramenta1;
	private telas.componentes.CampoTexto ctPlaca;
	private javax.swing.JScrollPane jspTabela;
	private telas.componentes.Painel pnlCadastro;
	private telas.componentes.Painel pnlFiltro;
	private telas.componentes.Painel pnlPrincipal;
	private telas.componentes.Rotulo rotulo1;
	private telas.componentes.Tabela tblVeiculos;
	// End of variables declaration//GEN-END:variables

	// Outras

}